﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.Dataset;
using CommonData;

namespace DataCentral.Forms
{
    public partial class DebtAdjustForm : CommonData.CommonChildForm
    {
        private DataCentralDataset.Debt_ARSaleDocRow _DRSelected;
        public DebtAdjustForm()
        {
            InitializeComponent();
        }

        private void PaidDefaultDebt()
        {
            DataCentralDataset.DebtAdjustRow drAdjust = null;
            foreach (DataCentralDataset.Debt_ARSaleDocRow drArDOC in dsCentral.Debt_ARSaleDoc)
            {
                if (!drArDOC.IsSelectedNull() && drArDOC.Selected && drArDOC.DocBal >0)
                {
                    drAdjust = dsCentral.DebtAdjust.NewDebtAdjustRow();
                    drAdjust.DebtNumber = string.Format("{0}-{1}", drArDOC.OrderType,drArDOC.OrderNbr);
                    drAdjust.PayDate = DateTime.Now.Date;
                    drAdjust.CustomerID = drArDOC.CustomerID;
                    drAdjust.SalePersonID = drArDOC.RoundID;
                    drAdjust.OrderNbr = drArDOC.OrderNbr;
                    drAdjust.PayAmount = drArDOC.DocBal;
                    drAdjust.Description = "Thanh toán hết";
                    dsCentral.DebtAdjust.AddDebtAdjustRow(drAdjust);
                    drArDOC.ReceiveTotal += drArDOC.DocBal;
                    drArDOC.DocBal = 0;

                }
            }
            bsDebtAdjust.EndEdit();
            daDebtAdjust.Update(dsCentral.DebtAdjust);
            daDebtARDoc.Update(dsCentral.Debt_ARSaleDoc);
        }

        private void DebtAdjustForm_Load(object sender, EventArgs e)
        {
            ValueListClass clValueList = new ValueListClass();
            dtpFromDate.DateTime = DateTime.Now.Date;
            dtpToDate.DateTime = DateTime.Now.Date;
            daDebtARDoc.Fill(dsCentral.Debt_ARSaleDoc, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
            iluCustomer.DataSource = clValueList.GetCustomerList();
            iluCustomer1.DataSource = iluCustomer.DataSource;
            iluSalePerson.DataSource = clValueList.GetSaleSysPersonList();
            iluSalePerson1.DataSource = iluSalePerson.DataSource;

            rdgDebtType_SelectedIndexChanged(null, null);
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            daDebtARDoc.Fill(dsCentral.Debt_ARSaleDoc, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
        }

        private void viewDebt_ARDoc_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (viewDebt_ARDoc.IsDataRow(viewDebt_ARDoc.FocusedRowHandle) &&
                viewDebt_ARDoc.GetFocusedDataRow() is DataCentralDataset.Debt_ARSaleDocRow)
            {
                _DRSelected = (DataCentralDataset.Debt_ARSaleDocRow)viewDebt_ARDoc.GetFocusedDataRow();
                daDebtAdjust.Fill(dsCentral.DebtAdjust, _DRSelected.OrderNbr);
                grpDebtAdjust.Text = string.Format("Thông tin chi tiết các lần thu nợ của đơn hàng ({0}-{1})", _DRSelected.OrderType, _DRSelected.OrderNbr);
            }
            else
            {
                _DRSelected = null;
                dsCentral.DebtAdjust.Clear();
            }

            gridDebtAdjust.Enabled = _DRSelected != null && _DRSelected.DocBal > 0;

        }

        private void btnDefaultPaid_Click(object sender, EventArgs e)
        {
            if (MessageClass.ShowQuestionMessage("Cập nhật mặc định sẽ tạo đơn thu nợ với giá trị bằng giá trị công nợ khách hàng hiện tại.\nBạn có đồng ý không?") == DialogResult.Yes)
            {
                PaidDefaultDebt();
            }
        }

        private void viewDebtAdjust_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            if (viewDebtAdjust.GetFocusedDataRow() is DataCentralDataset.DebtAdjustRow && _DRSelected!=null)
            {
                DataCentralDataset.DebtAdjustRow drDebtAdjust = (DataCentralDataset.DebtAdjustRow)viewDebtAdjust.GetFocusedDataRow();
                drDebtAdjust.DebtNumber = string.Format("{0}-{1}", _DRSelected.OrderType, _DRSelected.OrderNbr);
                drDebtAdjust.PayDate = DateTime.Now.Date;
                drDebtAdjust.CustomerID = _DRSelected.CustomerID;
                drDebtAdjust.SalePersonID = _DRSelected.RoundID;
                drDebtAdjust.OrderNbr = _DRSelected.OrderNbr;
                drDebtAdjust.PayAmount = _DRSelected.DocBal;
                drDebtAdjust.Description = "Thanh toán hết";
            }
        }

        private void viewDebtAdjust_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            MessageClass.ShowErrorMessage(ConstantClass._Error_InvalidData);
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void viewDebtAdjust_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            if (colPayAmount.SummaryItem.SummaryValue != null)
            {
                _DRSelected.ReceiveTotal = Convert.ToDecimal(colPayAmount.SummaryItem.SummaryValue);
                _DRSelected.DocBal = _DRSelected.LineAmt - Convert.ToDecimal(colPayAmount.SummaryItem.SummaryValue);
                if (_DRSelected.DocBal < 0)
                    _DRSelected.DocBal = 0;
            }
            bsDebtAdjust.EndEdit();
            bsDebt_ARDoc.EndEdit();
            daDebtAdjust.Update(dsCentral.DebtAdjust);
            daDebtARDoc.Update(dsCentral.Debt_ARSaleDoc);
        }

        private void viewDebtAdjust_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            if (viewDebtAdjust.GetFocusedDataRow() is DataCentralDataset.DebtAdjustRow)
            {
                DataCentralDataset.DebtAdjustRow drDebtAdjust = (DataCentralDataset.DebtAdjustRow)viewDebtAdjust.GetFocusedDataRow();
                if (drDebtAdjust.PayAmount > _DRSelected.DocBal)
                {
                    viewDebtAdjust.SetColumnError(colPayAmount, "Giá trị thu nợ không được lớn hơn giá trị còn nợ.");
                    viewDebtAdjust.FocusedColumn = colPayAmount;
                    e.Valid = false;
                }
            }
        }

        private void viewDebtAdjust_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                btnDelete.PerformClick();
            }
            else if (e.KeyCode == Keys.Insert)
            {
                btnAdd.PerformClick();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            bsDebtAdjust.EndEdit();
            daDebtAdjust.Update(dsCentral.DebtAdjust);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (viewDebtAdjust.GetFocusedDataRow() is DataCentralDataset.DebtAdjustRow)
            {
                DataCentralDataset.DebtAdjustRow drDebtAdjust = (DataCentralDataset.DebtAdjustRow)viewDebtAdjust.GetFocusedDataRow();
                if (MessageClass.ShowQuestionMessage("Bạn có muốn xóa đơn thu nợ này không?") == DialogResult.Yes)
                {
                    _DRSelected.DocBal += drDebtAdjust.PayAmount;
                    _DRSelected.ReceiveTotal -= drDebtAdjust.PayAmount;
                    drDebtAdjust.Delete();
                }
                bsDebtAdjust.EndEdit();
                daDebtAdjust.Update(dsCentral.DebtAdjust);
                daDebtARDoc.Update(dsCentral.Debt_ARSaleDoc);
            }
        }

        private void rdgDebtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.viewDebt_ARDoc.SortInfo.Clear();
            viewDebt_ARDoc.GroupCount = 1;
            if (rdgDebtType.SelectedIndex == 0)
            {
                this.viewDebt_ARDoc.SortInfo.AddRange(new DevExpress.XtraGrid.Columns.GridColumnSortInfo[] {
                new DevExpress.XtraGrid.Columns.GridColumnSortInfo(this.colRoundID, DevExpress.Data.ColumnSortOrder.Ascending)});
                //ND+, NT-), (IN+, CM-), (CI+, CC-
                colOrderType.FilterInfo = new DevExpress.XtraGrid.Columns.ColumnFilterInfo
                    ("OrderType = 'ND' OR OrderType = 'NT' OR OrderType = 'IN' OR OrderType = 'CM' OR OrderType = 'CI' OR OrderType = 'CC'");
            }
            else
            {
                this.viewDebt_ARDoc.SortInfo.AddRange(new DevExpress.XtraGrid.Columns.GridColumnSortInfo[] {
                new DevExpress.XtraGrid.Columns.GridColumnSortInfo(this.colCustomerID, DevExpress.Data.ColumnSortOrder.Ascending)});
                //(IN+, CM-), (CI+, CC-), (NP+, NM-), (NI+, NC-)
                colOrderType.FilterInfo = new DevExpress.XtraGrid.Columns.ColumnFilterInfo
                    ("OrderType = 'IN' OR OrderType = 'CM' OR OrderType = 'CI' OR OrderType = 'CC' OR OrderType = 'NP' OR OrderType = 'NM' OR OrderType = 'NI' OR OrderType = 'NC'");
            }
            viewDebt_ARDoc.ExpandAllGroups();
        }

        private void btnDebtSum_Click(object sender, EventArgs e)
        {
            DebtSumForm frmDebtSum = new DebtSumForm();
            frmDebtSum.ShowDialog();
        }
    }
}
